package com.zmc.lostfound.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.lostfound.common.exception.MyException;
import com.zmc.lostfound.common.utils.PageParams;
import com.zmc.lostfound.common.utils.PageResult;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.system.vo.NoticeSaveVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.lostfound.system.dao.NoticeDao;
import com.zmc.lostfound.system.entity.NoticeEntity;
import com.zmc.lostfound.system.service.NoticeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ZMC
 * @description 系统公告业务实现
 * @email 2869581855@qq.com
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, NoticeEntity> implements NoticeService {

    /**
     * 分页模糊查询系统公告
     *
     * @param pageParams 分页参数
     * @return PageResult
     */
    @Override
    public PageResult queryPage(PageParams pageParams) {
        Integer pageNo = pageParams.getPageNo();
        Integer pageSize = pageParams.getPageSize();
        String keyword = pageParams.getKeyword();
        Page<NoticeEntity> page = new Page<>(pageNo, pageSize);
        QueryWrapper<NoticeEntity> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword)) {
            queryWrapper.eq("id", keyword)
                    .or()
                    .eq("user_id", keyword)
                    .or()
                    .like("title", keyword);
        }
        queryWrapper.orderByDesc("id");
        return new PageResult(this.page(page, queryWrapper));
    }

    /**
     * 按id查询系统公告
     *
     * @param id 系统公告id
     * @return NoticeEntity
     */
    @Override
    public NoticeEntity queryInfoById(Long id) {
        if (id == null || id <= 0) {
            MyException.throwException("查询失败: 系统公告id不能为null且必须大于0");
        }
        return this.getById(id);
    }

    /**
     * 查询全部系统公告
     *
     * @return List<NoticeEntity>
     */
    @Override
    public List<NoticeEntity> findAll() {
        return this.query().orderByDesc("id").list();
    }

    /**
     * 新增系统公告
     *
     * @param noticeSaveVO 系统公告新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertNotice(NoticeSaveVO noticeSaveVO) {
        NoticeEntity noticeEntity = BeanUtil.copyProperties(noticeSaveVO, NoticeEntity.class);
        if (!this.save(noticeEntity)) {
            MyException.throwException("新增公告失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 修改系统公告
     *
     * @param noticeSaveVO 系统公告新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateNotice(NoticeSaveVO noticeSaveVO) {
        NoticeEntity noticeEntity = BeanUtil.copyProperties(noticeSaveVO, NoticeEntity.class);
        if (!this.updateById(noticeEntity)) {
            MyException.throwException("修改公告失败");
        }
        return Result.ok("修改成功");
    }

    /**
     * 批量删除系统公告
     *
     * @param noticeIds 系统公告ids
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteBatchNotice(List<Long> noticeIds) {
        if (CollectionUtil.isEmpty(noticeIds)) {
            MyException.throwException("删除公告失败: 公告id不能为空");
        }
        this.removeByIds(noticeIds);
        return Result.ok("删除成功");
    }

}